package com.kitty.game.fight.util;

import com.kitty.game.pet.model.Pet;
import com.kitty.game.skill.model.RoleSkill;
import com.kitty.game.fight.bean.Fight;
import com.kitty.game.fight.bean.FightObject;
import com.kitty.game.fight.bean.PetMirrorFightObject;
import com.kitty.game.fight.artifact.passive.ArtifactType;
import com.kitty.game.utils.Const;
import org.slf4j.LoggerFactory;

import java.util.Random;

/**
 * 计算战斗各种随机值
 */
public class FightRateUtil {

    public static int getRandHurt(int power) {
        Random random = new Random();
        int per = random.nextInt(10) + 1;
        int value = new Double(power * per * 0.0055).intValue();
        if (value <= 0) {
            return 1;
        }
        return random.nextInt(value);
    }

    public static boolean isHit(int score, int maxScore) {
        Random random = new Random();
        int value = random.nextInt(maxScore);
        return value <= score;
    }

    /**
     * 计算宠物能否复活
     *
     * @param fightObject
     * @return
     */
    public static boolean petCanRevive(FightObject fightObject) {
        if (fightObject == null) {
            return false;
        }
        if (!fightObject.isPet() && !fightObject.isPetMirror()) {
            return false;
        }
        if (!fightObject.isFallDown()) {
            return false;
        }
        if (fightObject.getCurrLife() > 0) {
            return false;
        }
        Pet pet = fightObject.getPet();
        int qinmi = pet.getIntimacy();
        Random random = new Random();
        if (pet.getType() == 2) {
            // 普通
            if (qinmi >= 500000) {
                if (fightObject.getReviveSize() >= 3) {
                    return false;
                }
                int tem = random.nextInt(100);
                if (tem < 20) {
                    fightObject.setReviveSize(fightObject.getReviveSize() + 1);
                    return true;
                } else {
                    return false;
                }
            } else if (qinmi >= 300000) {
                if (fightObject.getReviveSize() >= 2) {
                    return false;
                }
                int tem = random.nextInt(100);
                if (tem < 15) {
                    fightObject.setReviveSize(fightObject.getReviveSize() + 1);
                    return true;
                } else {
                    return false;
                }
            } else if (qinmi >= 100000) {
                if (fightObject.getReviveSize() >= 1) {
                    return false;
                }
                int tem = random.nextInt(100);
                if (tem < 5) {
                    fightObject.setReviveSize(fightObject.getReviveSize() + 1);
                    return true;
                } else {
                    return false;
                }
            } else {
                return false;
            }
        } else if (pet.getType() == 3) {
            // 变异
            if (qinmi >= 5000000) {
                if (fightObject.getReviveSize() >= 4) {
                    return false;
                }
                int tem = random.nextInt(100);
                if (tem < 50) {
                    fightObject.setReviveSize(fightObject.getReviveSize() + 1);
                    return true;
                } else {
                    return false;
                }
            } else if (qinmi >= 2000000) {
                if (fightObject.getReviveSize() >= 3) {
                    return false;
                }
                int tem = random.nextInt(100);
                if (tem < 50) {
                    fightObject.setReviveSize(fightObject.getReviveSize() + 1);
                    return true;
                } else {
                    return false;
                }
            } else if (qinmi >= 1000000) {
                if (fightObject.getReviveSize() >= 3) {
                    return false;
                }
                int tem = random.nextInt(100);
                if (tem < 40) {
                    fightObject.setReviveSize(fightObject.getReviveSize() + 1);
                    return true;
                } else {
                    return false;
                }
            } else if (qinmi >= 500000) {
                if (fightObject.getReviveSize() >= 3) {
                    return false;
                }
                int tem = random.nextInt(100);
                if (tem < 30) {
                    fightObject.setReviveSize(fightObject.getReviveSize() + 1);
                    return true;
                } else {
                    return false;
                }
            } else if (qinmi >= 300000) {
                if (fightObject.getReviveSize() >= 2) {
                    return false;
                }
                int tem = random.nextInt(100);
                if (tem < 20) {
                    fightObject.setReviveSize(fightObject.getReviveSize() + 1);
                    return true;
                } else {
                    return false;
                }
            } else if (qinmi >= 100000) {
                if (fightObject.getReviveSize() >= 1) {
                    return false;
                }
                int tem = random.nextInt(100);
                if (tem < 10) {
                    fightObject.setReviveSize(fightObject.getReviveSize() + 1);
                    return true;
                } else {
                    return false;
                }
            } else {
                return false;
            }
        } else if (pet.getType() == 4) {
            // 神兽
            if (qinmi >= 5000000) {
                if (fightObject.getReviveSize() >= 5) {
                    return false;
                }
                int tem = random.nextInt(100);
                if (tem < 60) {
                    fightObject.setReviveSize(fightObject.getReviveSize() + 1);
                    return true;
                } else {
                    return false;
                }
            } else if (qinmi >= 2000000) {
                if (fightObject.getReviveSize() >= 5) {
                    return false;
                }
                int tem = random.nextInt(100);
                if (tem < 55) {
                    fightObject.setReviveSize(fightObject.getReviveSize() + 1);
                    return true;
                } else {
                    return false;
                }
            } else if (qinmi >= 1000000) {
                if (fightObject.getReviveSize() >= 4) {
                    return false;
                }
                int tem = random.nextInt(100);
                if (tem < 44) {
                    fightObject.setReviveSize(fightObject.getReviveSize() + 1);
                    return true;
                } else {
                    return false;
                }
            } else if (qinmi >= 500000) {
                if (fightObject.getReviveSize() >= 3) {
                    return false;
                }
                int tem = random.nextInt(100);
                if (tem < 33) {
                    fightObject.setReviveSize(fightObject.getReviveSize() + 1);
                    return true;
                } else {
                    return false;
                }
            } else if (qinmi >= 300000) {
                if (fightObject.getReviveSize() >= 2) {
                    return false;
                }
                int tem = random.nextInt(100);
                if (tem < 22) {
                    fightObject.setReviveSize(fightObject.getReviveSize() + 1);
                    return true;
                } else {
                    return false;
                }
            } else if (qinmi >= 100000) {
                if (fightObject.getReviveSize() >= 1) {
                    return false;
                }
                int tem = random.nextInt(100);
                if (tem < 11) {
                    fightObject.setReviveSize(fightObject.getReviveSize() + 1);
                    return true;
                } else {
                    return false;
                }
            } else {
                return false;
            }
        }
        return false;
    }

    /**
     * 障碍的概率
     *
     * @param attacker
     * @param target
     * @return
     */
    public static int getZhangAiRate(Fight fight, FightObject attacker, FightObject target) {
        RoleSkill roleSkill = attacker.getSkillBy(attacker.getSkillId());
        int result = 0;
        Random random = new Random();
        int value = random.nextInt(100);

        if (target.isMonster() && fight.getType() == Const.fightType_monster) {
            return 100;
        }

        int targetTao = target.getTao();/**需要取道行的可以继承*/
        int selfTao = attacker.getTao();
        int targetValue = 0;
        if (attacker.getPolar() == Const.SCHOOL_METAL) {
            // 我方道行+强力障碍触发几率（增加2000年临时道行）+忽视抗封*3 + 技能等级*0.2 - 敌方道行+抗封*3
            if (target.getXinjun() != null) {
                targetValue = target.getLevel() * targetTao * 360 + Integer.parseInt(target.getXinjun().getResist_forgotten()) * 360* 4;
            } else if (target.getBossSet() != null) {
                targetValue = target.getLevel() * targetTao * 360
                        + target.getBossSet().getResist_forgotten() * 360* 4;
            } else if (target.isGuard()) {
                FightObject host = fight.queryFightObject(target.getHostId());
                if (host != null) {
                    targetValue = host.getRole().getTao() + host.getRole().getResistForgotten() * 360* 4;
                }
            } else {
                if (target.isPlayer()) {
                    targetValue = targetTao + target.getRole().getResistForgotten() * 360* 4;
                } else if (target.isPet()) {
                    int martial = (fight.isUseMonth() ? target.getPet().getMonthMartial() : target.getPet().getMartial()) / 2;
                    targetValue = martial + target.getPet().getResistForgotten() * 360* 4;
                }
            }
            if (attacker.isPlayer()) {
                result = selfTao
                        + (attacker.getRole().getStrengthForgotten() > value ? (2000 * 360) : 0)
                        + attacker.getRole().getIgnoreResistForgotten() * 360* 4 + roleSkill.getLevel() * 80 - targetValue;
            }
        }
        if (attacker.getPolar() == Const.SCHOOL_WOOD) {
            if (target.getXinjun() != null) {
                targetValue = target.getLevel() * targetTao * 360
                        + Integer.parseInt(target.getXinjun().getResist_poison()) * 360* 4;
            } else if (target.getBossSet() != null) {
                targetValue = target.getLevel() * targetTao * 360
                        + target.getBossSet().getResist_poison() * 360* 4;
            } else if (target.isGuard()) {
                FightObject host = fight.queryFightObject(target.getHostId());
                if (host != null) {
                    targetValue = host.getRole().getTao() + host.getRole().getResistPoison() * 360* 4;
                }
            } else {
                if (target.isPlayer()) {
                    targetValue = targetTao + target.getRole().getResistPoison() * 360* 4;
                } else if (target.isPet()) {
                    int martial = (fight.isUseMonth() ? target.getPet().getMonthMartial() : target.getPet().getMartial()) / 2;
                    targetValue = martial + target.getPet().getResistPoison() * 360* 4;
                }
            }
            if (attacker.isPlayer()) {
                result = selfTao
                        + (attacker.getRole().getStrengthPoison() > value ? (2000 * 360) : 0)
                        + attacker.getRole().getIgnoreResistPoison() * 360* 4 + roleSkill.getLevel() * 80 - targetValue;
            }
        }
        if (attacker.getPolar() == Const.SCHOOL_WATER) {
            if (target.getXinjun() != null) {
                targetValue = target.getLevel() * targetTao * 360
                        + Integer.parseInt(target.getXinjun().getResist_frozen()) * 360* 4;
            } else if (target.getBossSet() != null) {
                targetValue = target.getLevel() * targetTao * 360
                        + target.getBossSet().getResist_frozen() * 360* 4;
            } else if (target.isGuard()) {
                FightObject host = fight.queryFightObject(target.getHostId());
                if (host != null) {
                    targetValue = host.getRole().getTao() + host.getRole().getResistFrozen() * 360* 4;
                }
            } else {
                if (target.isPlayer()) {
                    targetValue = targetTao + target.getRole().getResistFrozen() * 360* 4;
                } else if (target.isPet()) {
                    int martial = (fight.isUseMonth() ? target.getPet().getMonthMartial() : target.getPet().getMartial()) / 2;
                    targetValue = martial + target.getPet().getResistFrozen() * 360* 4;
                }
            }

            if (attacker.isPlayer()) {
                result = selfTao
                        + (attacker.getRole().getStrengthFrozen() > value ? (2000 * 360) : 0)
                        + attacker.getRole().getIgnoreResistFrozen() * 360* 4 + roleSkill.getLevel() * 80 - targetValue;
            }

        }

        if (attacker.getPolar() == Const.SCHOOL_FIRE) {
            if (target.getXinjun() != null) {
                targetValue = target.getLevel() * targetTao * 360
                        + Integer.parseInt(target.getXinjun().getResist_sleep()) * 360* 4;
            } else if (target.getBossSet() != null) {
                targetValue = target.getLevel() * targetTao * 360
                        + target.getBossSet().getResist_sleep() * 360* 4;
            } else if (target.isGuard()) {
                FightObject host = fight.queryFightObject(target.getHostId());
                if (host != null) {
                    targetValue = host.getRole().getTao() + host.getRole().getResistSleep() * 360* 4;
                }
            } else {
                if (target.isPlayer()) {
                    targetValue = targetTao + target.getRole().getResistSleep() * 360* 4;
                } else if (target.isPet()) {
                    int martial = (fight.isUseMonth() ? target.getPet().getMonthMartial() : target.getPet().getMartial()) / 2;
                    targetValue = martial + target.getPet().getResistSleep() * 360* 4;
                }
            }

            if (attacker.isPlayer()) {
                result = selfTao
                        + (attacker.getRole().getStrengthSleep() > value ? (2000 * 360) : 0)
                        + attacker.getRole().getIgnoreResistSleep() * 360* 4 + roleSkill.getLevel() * 80 - targetValue;
            }
        }
        if (attacker.getPolar() == Const.SCHOOL_EARTH) {
            if (target.getXinjun() != null) {
                targetValue = target.getLevel() * targetTao * 360
                        + Integer.parseInt(target.getXinjun().getResist_confusion()) * 360* 4;
            } else if (target.getBossSet() != null) {
                targetValue = target.getLevel() * targetTao * 360
                        + target.getBossSet().getResist_confusion() * 360* 4;
            } else if (target.isGuard()) {
                FightObject host = fight.queryFightObject(target.getHostId());
                if (host != null) {
                    targetValue = host.getRole().getTao() + host.getRole().getResistConfusion() * 360* 4;
                }
            } else {
                if (target.isPlayer()) {
                    targetValue = targetTao + target.getRole().getResistConfusion() * 360* 4;
                } else if (target.isPet()) {
                    int martial = (fight.isUseMonth() ? target.getPet().getMonthMartial() : target.getPet().getMartial()) / 2;
                    targetValue = martial + target.getPet().getResistConfusion() * 360* 4;
                }
            }

            if (attacker.isPlayer()) {
                result = selfTao
                        + (attacker.getRole().getStrengthConfusion() > value ? (2000 * 360) : 0)
                        + attacker.getRole().getIgnoreResistConfusion() * 360* 4 + roleSkill.getLevel() * 80 - targetValue;
            }
        }

        if (target instanceof PetMirrorFightObject) {
            targetValue = targetTao;
        }
//        LoggerFactory.getLogger("概率").error("=={}=={}",attacker.getName(),target.getName());
        if (attacker.getXinjun() != null) {
            result = attacker.getLevel() * selfTao * 360 - targetValue;
        } else if (attacker.getBossSet() != null) {
            /**超级boss，道行值100*/
            if (attacker.getBossSet().getName().equals("七杀星君")){
                result = attacker.getLevel() * selfTao * 360 - targetValue;
            }else {
                selfTao = attacker.isSuperBoss() ? 100 : selfTao;
                result = attacker.getLevel() * selfTao * 360 - targetValue;
                if("财神".equals(attacker.getName()) || "福星".equals(attacker.getName())){
                    result = 200*360;
                }
            }
        } else if (attacker.isRoleMirror()) {
            result = selfTao - targetValue;
        }

        // 法宝神火罩抗性
        int coverArtifactEffectRate = (int) ArtifactType.COVER.getEffect().trigger(fight, target);
        if (coverArtifactEffectRate > 0) {
            // 只要穿戴神火罩，就波特效
            ArtifactType.COVER.getEffect().broadCast(fight, target);
        }
        result -= coverArtifactEffectRate;
        if (result < -300 * 360) {
            return 0;
        }
        if (result <= -100 * 360) {
            return 50;// 只能障碍一回合
        }
        if (result <= 100 * 360) {
            return 80;
        }
        return 100;
    }

    /**
     * 计算增益后道行
     */
    public static double calcTaoRate(int rate){
        double s = 1+(rate*0.01/4);
        return s;
    }

    /**
     * 计算法攻必杀率
     *
     * @param fightObject
     * @return
     */
    public static boolean calcMagicStuntRate(FightObject fightObject, RoleSkill roleSkill) {
        Random random = new Random();
        // 计算人物法攻必杀率
        if (roleSkill.getType() == 146) {
            if (fightObject.isPlayer()) {
                if ((random.nextInt(100) + 1) <= fightObject.getRole().getMStuntRate()) {// 法术必杀
                    return true;
                }
            } else if (fightObject.isPet()) {
                if ((random.nextInt(100) + 1) <= fightObject.getExtraAttr().getInt("stunt")) {
                    return true;
                }
            } else if ((random.nextInt(100) + 1) <= 5) {// 野怪的定5%
                return true;
            }
        }
        return false;
    }

    /**
     * 计算法攻破防率
     *
     * @param fightObject
     * @return
     */
    public static boolean calcMagicPenetrateRate(FightObject fightObject, RoleSkill roleSkill) {
        Random random = new Random();
        if (roleSkill.getType() == 146) {
            if (fightObject.isPlayer()) {
                if ((random.nextInt(100) + 1) <= fightObject.getRole().getPenetrateRate()) {// 破防率
                    return true;
                }
            }
        }
        return false;
    }


    public static boolean isPetSkillHit(FightObject effector, FightObject target) {
//    施法目标有可能是宠物，有可能是角色（亲密无间角色就可以释放舍命一击技能）！
//    所以取：
//    如果施法是宠物   被施法目标是宠物。
//    则：
//    施法目标武学-被施法武学≥0 舍命成功100%
//            -100000≤施法目标武学-被施法武学<0  舍命成功 70%
//            -400000<施法目标武学-被施法武学<-100000     舍命成功 50%
//    施法目标武学-被施法武学<-400000 舍命成功 10%

//    如果是施法是角色 被施法目标是宠物
//    则：
//    施法目标道行-被施法武学≥0  舍命成功100%
//            -100000≤施法目标道行-被施法武学<0  舍命成功 70%
//            -400000<施法目标道行-被施法武学<-100000     舍命成功 50%
//    施法目标道行-被施法武学≤-400000  舍命成功 10%

//    如果是施法目标是角色 被施法目标是角色
//    则：
//    施法目标道行-被施法道行≥0  舍命成功100%
//            -100000≤施法目标道行-被施法道行<0  舍命成功 70%
//            -400000<施法目标道行-被施法道行<-100000     舍命成功 50%
//    施法目标道行-被施法道行≤-400000 舍命成功 10%
//    如果施法目标是宠物 被施法目标是角色，
//    则：
//    施法目标武学-被施法道行≥0  舍命成功100%
//            -100000≤施法目标道行-被施法道行<0  舍命成功 70%
//            -400000<施法目标道行-被施法道行<-100000     舍命成功 50%
//    施法目标道行-被施法道行≤-400000 舍命成功 10%
        int effectorScore = effector.isPlayer() ? effector.getRole().getTao() : effector.getPet().getMartial();
        int targetScore = target.isPet() ? target.getPet().getMartial() : target.getTao();
        int diff = effectorScore - targetScore;
        if (diff >= 0) {
            return true;
        }
        if (-100000 <= diff && diff < 0) {
            return FightRateUtil.isHit(70, 100);
        }
        if (-400000 <= diff && diff < -100000) {
            return FightRateUtil.isHit(50, 100);
        }
        return FightRateUtil.isHit(10, 100);
    }


}
